
#ifndef __3DMATERIAL_H__
#define __3DMATERIAL_H__

#include "GameBitmap.h"
#include "3dRenderDef.h"
#include <vector>


// defines for materials, follow our polygon attributes as much as possible
#define MATV1_ATTR_2SIDED                 0x0001
#define MATV1_ATTR_TRANSPARENT            0x0002
#define MATV1_ATTR_8BITCOLOR              0x0004
#define MATV1_ATTR_RGB16                  0x0008
#define MATV1_ATTR_RGB24                  0x0010

#define MATV1_ATTR_SHADE_MODE_CONSTANT    0x0020
#define MATV1_ATTR_SHADE_MODE_EMMISIVE    0x0020 // alias
#define MATV1_ATTR_SHADE_MODE_FLAT        0x0040
#define MATV1_ATTR_SHADE_MODE_GOURAUD     0x0080
#define MATV1_ATTR_SHADE_MODE_FASTPHONG   0x0100
#define MATV1_ATTR_SHADE_MODE_TEXTURE     0x0200


// defines for material system
#define MAX_MATERIALS                     256


class C3dMaterial
{
public:

	C3dMaterial()
	:	state(0),id(0),attr(0),      
		ka(0), kd(0), ks(0), power(0),
		iaux1(0), iaux2(0), faux1(0), faux2(0),
		ptr(NULL)
	{
		memset(name, 0, sizeof(name));
		memset(texture_file, 0, sizeof(texture_file));

		memset(&ra, 0, sizeof(ra));
		memset(&rd, 0, sizeof(rd));
		memset(&rs, 0, sizeof(rs));

		memset(&color, 0, sizeof(color));
		texture.DestroyImage();
	}

	~C3dMaterial()
	{

	}

	int Reset();

	//public member
	int				state;           // state of material
	int				id;              // id of this material, index into material array
	char			name[64];       // name of material
	int				attr;           // attributes, the modes for shading, constant, flat, 
	// gouraud, fast phong, environment, textured etc.
	// and other special flags...

	RGBAV1			color;            // color of material
	float			ka, kd, ks, power; // ambient, diffuse, specular, 
	// coefficients, note they are 
	// separate and scalars since many 
	// modelers use this format
	// along with specular power

	RGBAV1			ra, rd, rs;       // the reflectivities/colors pre-
	// multiplied, to more match our 
	// definitions, each is basically
	// computed by multiplying the 
	// color by the k's, eg:
	// rd = color*kd etc.

	char			texture_file[80];   // file location of texture
	CBitMapImage	texture;    // actual texture map (if any)

	int				iaux1, iaux2;      // auxiliary vars for future expansion
	float			faux1, faux2;
	void			*ptr;
};

C3dMaterial materials[MAX_MATERIALS];	// materials in system
int num_materials;						// current number of material


class C3dMaterialMgr
{
public:

	typedef std::vector<C3dMaterial*>		MatVct;

	C3dMaterialMgr()
	{
		m_materials			= NULL;
		m_materialCount		= 0;
		num_materials		= 0;
	}

	~C3dMaterialMgr()
	{

	}

	int Init(PDxDrawEnv pDxDrawEnv, int materialCount = 256)
	{
		m_pDxDrawEnv	= pDxDrawEnv;
		m_materialCount	= materialCount;

		m_materials = new C3dMaterial[m_materialCount];

		return 0;
	}

	int Reset()
	{
		//
		for (int i=0; i<m_materialCount; i++)
			m_materials[i].Reset();

		delete[] m_materials;

		m_materials = NULL;

		return 0;
	}

	C3dMaterial* operator[] (int nIndex)
	{
		return &m_materials[nIndex];
	}

	//int Light_Obj4d_World16(CObj4d& obj,			// object to process
	//						int max_lights);		// maximum lights in list

	//int Light_RenderList_World16(CRenderList& rend_list,		// object to process
	//							int max_lights);					// maximum lights in list

	int					num_materials;			//current material nuber

protected:

	PDxDrawEnv			m_pDxDrawEnv;
	int					m_materialCount;
	

	C3dMaterial*		m_materials;
};

//typedef CObjInst< C3dMaterialMgr > MatMgrInst;

#endif